package com.wimcorp.magic.subtypes;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderColumn;

@Embeddable
public class SubTypeHolder extends SubType {

	
	@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	@JoinTable(name="playingcard_subtype",
    joinColumns=
        @JoinColumn(name="playingcard_id", referencedColumnName="id"),
    inverseJoinColumns=
        @JoinColumn(name="subtype_id", referencedColumnName="id")
    )
	@OrderColumn
	private Set<SubType> subTypes = new HashSet<SubType>();

	public void addSubType(SubType subType) {
		subTypes.add(subType);
	}

	public void removeSubType(SubType subType) {
		subTypes.remove(subType);
	}

	public Set<SubType> getSubTypes() {
		return subTypes;
	}
	
	public boolean hasSubType(SubType subType) {
		return subTypes.contains(subType);
	}

	public String toString() {
		StringBuilder subTypeHolder = new StringBuilder();
		int i = 0;
		for(SubType subType : subTypes){
			i++;
			subTypeHolder.append(subType.getName());
			if (i != subTypes.size()) {
				subTypeHolder.append(" ");
			}
		}
		return subTypeHolder.toString();
	}

	
}
